package com.sdx.controller;

import com.alibaba.fastjson.JSONObject;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.web.AuthorizedBaesController;
import com.ibeetl.admin.core.web.JsonResult;
import org.beetl.sql.core.SQLManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName ConsoleController
 * @Description
 * @Author kangbin
 * @Date 2020/3/3 15:46
 */
@Controller
@RequestMapping("/console")
public class ConsoleController extends AuthorizedBaesController {

    private static final String SQL = "console.work";

    //客户等待队列:1
    private static final String WAIT_QUEUE = "_crm_session_wait";

    @Resource
    private SQLManager sqlManager;

    @GetMapping("/index.do")
    @ResponseBody
    public ModelAndView index() {
        ModelAndView view = new ModelAndView( "/consoleIndex.html");
        return view;
    }

    @ResponseBody
    @RequestMapping("/getData.json")
    public JsonResult getData(HttpServletRequest request){
        JsonResult result = JsonResult.success();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
        CoreUser user = getLoginUser(request);
        Integer tenantId = 0;
        if (user!=null) {
            user.getTenantId();
        }
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 时间加上 00:00:00
        Date startTime = truncDate(calendar);
        // 时间加上 23:59:59
        Date endTime = fillTimes(calendar);
        calendar.add(Calendar.DATE,-1);
        // 时间加上 00:00:00
        Date yStartTime = truncDate(calendar);
        // 时间加上 23:59:59
        Date yEndTime = fillTimes(calendar);
        Map<String,Object> param = new HashMap<>();
        param.put("tenantId",tenantId);
        //订单总数
        Integer orderCount = sqlManager.intValue(SQL + ".selectOrderCount", param);
        //订单金额
        Integer orderMoney = sqlManager.intValue(SQL + ".selectOrderMoney", param);
        //总用户数
        Integer custCount = sqlManager.intValue(SQL + ".selectCustCount", param);

        //申请退款数
        Integer refundCount = sqlManager.intValue(SQL + ".selectApplyCount", param);

        //确认退款
        param.put("refundStatus",4);
        Integer eolRefundCount = sqlManager.intValue(SQL + ".selectRefundCount", param);

        //待处理
        param.put("status",0);
        Integer unHandSuggest = sqlManager.intValue(SQL + ".selectSuggestCount", param);

        //已处理
        param.put("status",1);
        Integer handSuggest = sqlManager.intValue(SQL + ".selectSuggestCount", param);


        param.put("startTime",sdf.format(startTime));
        param.put("endTime",sdf.format(endTime));

        //今天订单总数
        Integer tdayOrderCount = sqlManager.intValue(SQL + ".selectOrderCount", param);
        // 今天订单金额
        Integer tdayOrderMoney = sqlManager.intValue(SQL + ".selectOrderMoney", param);
        //今日新增用户数
        Integer tdayCustCount = sqlManager.intValue(SQL + ".selectCustCount", param);

        param.put("startTime",sdf.format(yStartTime));
        param.put("endTime",sdf.format(yEndTime));
        //昨天订单总数
        Integer ydayOrderCount = sqlManager.intValue(SQL + ".selectOrderCount", param);
        //昨天订单金额
        Integer ydayOrderMoney = sqlManager.intValue(SQL + ".selectOrderMoney", param);
        //昨日新增用户数
        Integer ydayCustCount = sqlManager.intValue(SQL + ".selectCustCount", param);


        JSONObject json = new JSONObject();
        json.put("orderCount",orderCount);
        json.put("tdayOrderCount",tdayOrderCount);
        json.put("ydayOrderCount",ydayOrderCount);

        json.put("orderMoney",orderMoney);
        json.put("tdayOrderMoney",tdayOrderMoney);
        json.put("ydayOrderMoney",ydayOrderMoney);

        json.put("custCount",custCount);
        json.put("tdayCustCount",tdayCustCount);
        json.put("ydayCustCount",ydayCustCount);

        //申请退款数
        json.put("refundCount",refundCount);

        //确认退款
        param.put("refundStatus",4);
        json.put("eolRefundCount",eolRefundCount);

        //今日确认退款
        param.put("startTime",sdf.format(startTime));
        param.put("endTime",sdf.format(endTime));
        json.put("tdayEolRefundCount",sqlManager.intValue(SQL + ".selectRefundCount", param));

        //昨日确认退款
        param.put("startTime",sdf.format(yStartTime));
        param.put("endTime",sdf.format(yEndTime));
        json.put("ydayEolRefundCount",sqlManager.intValue(SQL + ".selectRefundCount", param));


        json.put("handSuggest",handSuggest);
        json.put("unHandSuggest",unHandSuggest);

        //意见今日已处理
        param.put("status",1);
        param.put("startTime",sdf.format(startTime));
        param.put("endTime",sdf.format(endTime));
        json.put("tdaySuggest",sqlManager.intValue(SQL + ".selectSuggestCount", param));


        //意见昨日处理
        param.put("startTime",sdf.format(yStartTime));
        param.put("endTime",sdf.format(yEndTime));
        json.put("ydaySuggest",sqlManager.intValue(SQL + ".selectSuggestCount", param));

        result.setData(json);
        return result;
    }


    /**
     * 补充 00:00:00
     * @param cal
     * @return
     */
    public static Date truncDate(Calendar cal) {
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        return cal.getTime();
    }

    /**
     * 补充 23:59:59 999
     * @param cal
     * @return
     */
    public static Date fillTimes(Calendar cal) {
        cal.set(Calendar.HOUR_OF_DAY,23);
        cal.set(Calendar.MINUTE,59);
        cal.set(Calendar.SECOND,59);
        cal.set(Calendar.MILLISECOND,999);
        return cal.getTime();
    }

}
